'use strict';

app.controller('remarksCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$filter', '$location', '$timeout', 'lodash', 'editableOptions', 'editableThemes',  function ($scope, $http, $stateParams, $rootScope, $filter, $location, $timeout, lodash, editableOptions, editableThemes) {
    // editableThemes.bs3.inputClass = 'input-sm';
    // editableThemes.bs3.buttonsClass = 'btn-sm';
    // editableOptions.theme = 'bs3';
    $rootScope.initBreadcrumb($scope.breadcrumb);
    //页面加载时，给headerElement设置元素
    $rootScope.initHeaderElements($scope.settings);
    var backUrl = {
        show : true,
        label: "返回",
        func : function() {
            $rootScope.backHistory();
        }
    }
    $scope.settings.btnEle.push(backUrl);
    //结束headerElement设置

    $scope.info = {
        remarkThs: [],
        isMatrix: $rootScope.isMatrixRole()
    };
    
    setFlowHeight();

    function setFlowHeight() {
        $timeout(function() {        
            var h = document.documentElement.clientHeight || document.body.clientHeight;
            h = h - 155;
            $(".remark-content").css({
                "max-height": h +"px",
                "overflow": "auto",
            });
        }, 100)
    }

    $(window).resize(function() {
        setFlowHeight();
    });

    getRemarks();

    $scope.info.remarkThs = [
        {
            "thName": "类型",
            "width":"20%"
        }, {
            "thName": "内容",
            "width":"70%"
        }
    ];

    $scope.info.remarkList = {
        "tdObjs": [
                {
                    "tdName": "type",
                    "tdType": "select",
                    "typeList": {
                        "list": config_remark_type,
                        "key": "key",
                        "value": "value"
                    }
                },
                {
                    "tdName": "description",
                    "tdType": "text"
                },
            ],
        "trIsClick": false
    };
    $scope.info.remarkList.datas=[];

    $scope.reqDetails = {};
    var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
    $http.get(url).success(function(result) {
        if(result.code == "0") {
            if(result.data) {
                $scope.reqDetails = result.data[0];
                $scope.breadcrumb.push('产品设计',$scope.reqDetails.reqName);
            }
        }
    });

    //获取要求集合
    function getRemarks() {
        if(!$stateParams.reqId) {
            $rootScope.toasterErrorInfo("获取要求时reqId未定义！");
            return false;
        }
        var url = matrix_product_url +"/reqRemark/getReqRemarks?reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.info.remarkList.datas=result.data;
                } else {
                    $rootScope.toasterErrorInfo("获取要求数据错误！");
                }
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

    //添加要求
    $scope.addRemark = function() {
        var postParams = {
            reqId : $stateParams.reqId,
        };
        var url = matrix_product_url +"/reqRemark/saveReqRemark";
        $http({
            method  : "post",
            url     : url,
            data    : JSON.stringify(postParams),
            headers : {"Content-Type": "application/json"}
        }).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    var remarkItem = {"id": result.data.id, "number": result.data.number, "reqId": result.data.reqId, "description": ""};
                    $scope.info.remarkList.datas.push(remarkItem);
                } else {
                    $rootScope.toasterErrorInfo("获取remark id失败！");
                }
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

    //更新要求
    $scope.updateRemark = function(remark) {
        var url = matrix_product_url +"/reqRemark/saveReqRemark";
        $http({
            method  : "post",
            url     : url,
            data    : JSON.stringify(remark),
            headers : {"Content-Type": "application/json"}
        }).success(function(result) {
            if(result.code == "0") {
                getRemarks()
                $rootScope.toasterSuccessInfo("更新成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

    $scope.deleteRemark = function(remark) {
        var url = matrix_product_url +"/reqRemark/deleteReqRemark?id="+ remark.id;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                lodash.remove($scope.info.remarkList, {"id": remark.id});
                $rootScope.toasterSuccessInfo("删除成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };

}]);
